<?php
$lastquestion = $this->session->userdata('LastQuestion_' . $data->id);
if ($lastquestion != "") {
    $lastquestion = json_decode(stripcslashes($lastquestion));
}
?>

<script src="<?php echo $baseurl; ?>public/js/Tvshow.js" type="text/javascript"></script>
<script src="<?php echo $baseurl; ?>public/js/analytics_.js" type="text/javascript"></script>
<script type="text/javascript" src="<?php echo $baseurl ?>/public/js/socket.io-1.2.0.js"></script>
<script>
    App.Base_Api = '<?php echo $baseapi ?>';
    var socket = null;
    var User = {
        id: <?php echo $this->session->userdata('id_mem'); ?>,
        username: '<?php echo $this->session->userdata('username_mem'); ?>',
        role: '<?php echo $this->session->userdata('role'); ?>',
        avatar: '<?php echo $this->session->userdata('avatar'); ?>',
        fullname: '<?php echo $this->session->userdata('fullname_mem'); ?>',
        point: 0,
        email: '<?php echo $this->session->userdata('email'); ?>',
        address: '<?php echo $this->session->userdata('address'); ?>',
        mobile: '<?php echo $this->session->userdata('mobile'); ?>',
        birthday: '<?php echo $this->session->userdata('birthday'); ?>',
        isLogin: function () {
            if (this.role == "member")
                return true;
            return false;
        }
    };

    Program.Base_Url_Socket = '<?php echo $socketNode ?>';
    Program.id_channel = <?php echo $data->channel_id ?>;
    Program.id_tvshow = <?php echo $data->id ?>;
    Program.background_show = "";
    Program.logo_channel = "";
    Program.name = '<?php echo $data->name ?>';
    Program.time = '<?php echo $data->schedule ?>';
    Program.type_qa = <?php echo $data->type ?>;
    Program.id_answer = <?php echo isset($lastquestion->id_answer) ? $lastquestion->id_answer : 0 ?>;
    Program.id_question = <?php echo isset($lastquestion->id_question) ? $lastquestion->id_question : 0 ?>;
    Program.other_opinion = <?php echo isset($lastquestion->other_opinion) ? $lastquestion->other_opinion : 0 ?>

    Program.before_question = <?php echo isset($lastquestion->id_question) ? $lastquestion->id_question : 0 ?>;
    Program.before_answer = <?php echo isset($lastquestion->id_answer) ? $lastquestion->id_answer : 0 ?>;
    Program.before_tvshow = <?php echo isset($lastquestion->id_tvshow) ? $lastquestion->id_tvshow : 0 ?>;
    Program.before_show = <?php echo isset($lastquestion->id_show) ? $lastquestion->id_show : 0 ?>;
    Program.before_user = <?php echo isset($lastquestion->id_user) ? $lastquestion->id_user : 0 ?>;


    Program.Answer.answered_number =<?php echo isset($lastquestion->answer->answered_number) ? $lastquestion->answer->answered_number : 0 ?>;
    Program.Answer.exactly_number = <?php echo isset($lastquestion->answer->exactly_number) ? $lastquestion->answer->exactly_number : 0 ?>;

    SocketConnection(function (socketUrl) {
        //console.log('socketUrl', socketUrl);
        socket = io.connect(socketUrl, {'force new connection': true});

        socket.on('connect', function () {
            //console.log("Connected: " + socketUrl);
            /*
             *@jointvshow Tham gia phòng
             *@connecttwo Nhận socket sau
             */
            socket.emit('jointvshow', Program.id_tvshow, User.id);
            socket.emit('connecttwo', Program.id_tvshow);
        });

        socket.on('disconnect', function () {
            //console.log("Disconnected");
            socket.io.connect();
        });

        socket.on('status_show', function (status) {
            //console.log("status_show - " + Program.id_tvshow + ": " + status);
            Program.status_show = status;
        });

        Program.runSocket();
        Program.runSocketStatistic();

        intro = new Audio('<?php echo $imgurl . $data->sound_welcome ?>');
        background = new Audio('<?php echo $imgurl . $data->sound_bg ?>');
        intro.play();
        intro.onended = function () {
            background.loop = true;
            background.play();
        };
        if (localStorage.getItem("dfd5") == 1) {
            if (intro != null)
                intro.pause();
            if (background != null)
                background.pause();
        }


    });

    $(function () {
        $(".btnStatistic").on("click", function () {
            $(this).hide();
            $(".btnBack").css("display", "inline-block");

            $(".Choices").hide();
            $(".Statistic").show();

            return false;
        });

        $(".btnBack").on("click", function () {
            $(this).hide();
            $(".btnStatistic").css("display", "inline-block");

            $(".Choices").show();
            $(".Statistic").hide();
            return false;
        });

        $('.pluginButton').on('click', function () {
            var _cover = $("#cover_img").val();
            var uri = '<?php echo $uriCurrent ?>';
            Toast.progress({});
            UI_Dialog.close();
            $.ajax({
                type: 'get',
                url: '<?php echo $baseurl ?>check-fb',
                data: {cover: _cover, user_id: User.id, show_name: Program.name, data_questions: JSON.stringify(Program.data_question), __show_bk: '<?php echo $this->security->get_csrf_hash() ?>'},
                success: function (et) {
                    if (et != "") {
                        var data = JSON.parse(et);

                        if (data.status == 200) {
                            Toast.progressNext();
                            $("#content_tvshow").attr('style', 'background:;display: block;').find('label').removeClass('label_2').parent('li.selected').find('label').removeClass('chosse');
                            UI_Dialog.showConfirm({
                                buttons: [
                                    {
                                        id: "Confirm_cancel",
                                        text: "Huỷ",
                                    },
                                    {
                                        id: "Confirm_ok",
                                        text: "Chia sẻ",
                                        type: "bold"
                                    }
                                ],
                                content: '<textarea id="content_sharefb" rows="3"></textarea>',
                                title: 'Chia sẻ',
                                width: "430px",
                                event: function () {
                                    $("#Confirm_text").focus();
                                    $("#Confirm_ok").on("click", function () {
                                        content_sharefb($("#content_sharefb").val(), User.id, uri, Program.id_answer);
                                    });
                                }
                            });
                        } else if (data.status == 201) {
                            window.location.href = data.url;
                        }
                    } else {
                        Toast.progressNextToToast({
                            text: "Chia sẻ không thành công. Vui lòng thử lại.",
                            id: "gv"
                        });
                    }
                }
            });
        });

        function content_sharefb(msg, userid, uri, id_answer) {
            Toast.progress({});
            $.ajax({
                type: 'get',
                url: '<?php echo $baseurl ?>share-fb',
                data: ({msg: msg, user_id: userid, uri: uri, choice: id_answer, __show_bk: '<?php echo $this->security->get_csrf_hash() ?>'}),
                success: function (et) {
                    Toast.progressNext();
                }
            });
        }
    });

</script>
<?php

?>
<?php
//print_r($lastquestion);
//echo '<pre>' . print_r($data);die();
//echo '<pre>'. print_r();
?>
<div id="tvshow">
    <div id="ajax_content">
        <div id="slide_tvshow">
            <?php
            $slide = $fview->isMobile() ? json_decode($data->slide_wap) : json_decode($data->slide_web);
            ?>
            <ul class="bxslider">
                <?php
                foreach ($slide as $item) {
                    echo '<li><img src="' . $imgurl . $item . '" alt=""/></li>';
                }
                ?>
            </ul>

            <div class="logo logo_wap" style="background: url(<?php echo $imgurl . $data->clogo ?>)"></div>
            <div class="statusbar">
                <div class="status play">
                    <div class="hidden">
                        <div class="text">Chào mừng đến với chương trình <?php echo $data->name ?></div>
                    </div>
                </div>
                <div class="info">
                    <div class="logo" style="background: url(<?php echo $imgurl . $data->clogo ?>)"></div>
                    <div class="schedule">
                        <p style="overflow: hidden;">
                            <span class="marquee" style="display: inline-block;">
                                <span class="icon-clock"></span>
                                <?php echo $data->schedule ?>
                            </span>
                        </p>
                    </div>
                </div>
            </div>
        </div>
        <div id="content_tvshow" class="clearfix">
            <input value="<?php echo $imgurl . $slide[0] ?>" id="cover_img" type="hidden" />
            <div class="Cover" style="display: none">
                <img style="width: 100%" src="<?php echo $imgurl . $slide[0] ?>" alt="">
            </div>
            <div class="Navigator">
                <a href="#" class="btnStatistic"></a>
                <a href="#" class="btnBack"></a>
            </div>
            <div class="Question"></div>
            <div class="Wrapp">
                <div class="Choices"></div>
                <div class="Statistic"></div>
            </div>
        </div>
    </div>
    <div id="comment_tvshow">
        <div class="relative clearfix" style="margin-bottom: 12px">
            <header class="floatLeft">BÌNH LUẬN</header>
            <div class="group-button floatRight">
                <div class="pluginButton pluginButtonSmall"></div>
                <div class="fb-like" data-href="https://developers.facebook.com/docs/plugins/" data-layout="button_count" data-action="like" data-show-faces="false" data-share="false"></div>
            </div>
        </div>
        <div class="fb-comments" data-href="http://developers.facebook.com/docs/plugins/comments/" data-width="100%" data-numposts="5"></div>
    </div>
</div>

<div id="fb-root"></div>
<script>(function (d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id))
            return;
        js = d.createElement(s);
        js.id = id;
        js.src = "//connect.facebook.net/vi_VN/sdk.js#xfbml=1&version=v2.0";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));</script>